package com.wutuo.roster.provider.dao;

import com.wutuo.roster.api.request.ListParentsRequest;
import com.wutuo.roster.api.request.ParentsRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SqlBuilder;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

/**
 * 服务关系表 dao sql 层
 *
 * @author lvh
 * @version 1.0
 */

public class ParentsSqlProvider extends BaseParentsSqlProvider {

    public String lisForPage(ListParentsRequest request) {
        BEGIN();
        getSelected();
        FROM("parents a ");

        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());

        if (request.getPage() != null && request.getSize() != null) {
            sql.append(" limit #{index,jdbcType=INTEGER} ,  #{size,jdbcType=INTEGER} ");
        }

        return sql.toString();
    }

    public String list(ParentsRequest request) {
        BEGIN();
        getSelected();
        FROM("parents a");
        if (!StringUtils.isEmpty(request.getRoasterIds())) {
            WHERE(" a.roster_id in (" + request.getRoasterIds() + ")");
        }

        if (!StringUtils.isEmpty(request.getIds())) {
            WHERE(" a.id in (" + request.getIds() + ")");
        }

        if (request.getInstitutionId() != null) {
            WHERE(" a.cm_institution_id  = #{institutionId,jdbcType=BIGINT}");
        }

        WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        return SQL();
    }

    public void getSelected() {
        SELECT(
            "a.id,roster_id as rosterId,user_name as userName,user_phone as userPhone,user_type as userType,create_by as createBy,create_date as createDate,update_by as updateBy,update_date as updateDate,remarks as remarks,del_flag as delFlag");
    }

}